<?php

class Model_Comments  extends ZloY_Model_CRUD{
	protected $_tableName;
	
	public function __construct($id , $tableName)
	{
		$this->_tableName = $tableName;
		parent::__construct($id);
	}
	
	public function getTable()
	{
		switch ($this->_tableName)
		{
			case 'place' : return new Model_DbTable_Comments_Place();
 		}
	}
	
	public function getItem($id)
	{
		switch($this->_tableName){
			case 'place' : return new Model_Place($id);
		}
	}
	
	public function addChildren($data)
	{
		$data['date'] = new Zend_Db_Expr('NOW()');
		$item = $this->getItem((int) $data['item_id']);
		$item->addCommentCount(1);
		if(isset($this->_row)){
			return $this->getTable()->createChildRow((int) $this->_row->id , $data)->save();
		} else {
			return $this->getTable()->createChildRow(0, $data)->save();
		}
	}
	
	public function getComments($item_id)
	{
		return $this->_table->fetchAll('item_id = ' . $item_id);
	}
}